package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.SysMenu;
import org.feng.entity.result.SysMenuTreeResult;
import org.feng.service.ISysMenuRoleService;
import org.feng.service.ISysUserService;
import org.feng.condition.Condition;
import org.feng.utils.TreeUtils;
import org.feng.annotation.AccessLog;
import org.feng.base.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.feng.service.ISysMenuService;
import org.feng.base.controller.BaseController;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.Arrays;

/**
 * 菜单表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/system/sysMenu")
@Tag(name = "菜单")
public class SysMenuController extends BaseController {

    @Resource
    private ISysMenuService sysMenuService;
    @Resource
    private ISysMenuRoleService sysMenuRoleService;
    @Resource
    private ISysUserService sysUserService;

    @GetMapping("/menuTree")
    @Operation(summary = "菜单树")
    public Result<List<SysMenuTreeResult>> menuTree() {
        List<SysMenu> list = sysMenuService.list(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getSort));
        List<SysMenuTreeResult> treeResultList = toBeanList(list, SysMenuTreeResult.class);
        List<SysMenuTreeResult> resultList = TreeUtils.treeOut(treeResultList, SysMenuTreeResult::getId, SysMenuTreeResult::getParentId, SysMenuTreeResult::getChildren);
        return Result.success(resultList);
    }

    @GetMapping("/createButton")
    @AccessLog("生成按钮")
    @Operation(summary = "生成按钮")
    public Result<Boolean> createButton(@RequestParam String ids) {
        sysMenuService.createButton(ids);
        return Result.success();
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysMenu>> pageList(SysMenu sysMenu) {
        return Result.success(sysMenuService.page(Condition.getPage(), Wrappers.lambdaQuery(sysMenu)));
    }

    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysMenu>> list(SysMenu sysMenu) {
        return Result.success(sysMenuService.list(Wrappers.lambdaQuery(sysMenu)));
    }

    @PostMapping("/save")
    @AccessLog("保存菜单")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody SysMenu sysMenu) {
        return Result.status(sysMenuService.save(sysMenu));
    }

    @PostMapping("/update")
    @AccessLog("更新菜单")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysMenu sysMenu) {
        return Result.status(sysMenuService.updateById(sysMenu));
    }

    @GetMapping("/remove")
    @AccessLog("删除菜单")
    @Operation(summary = "删除列表")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysMenuService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
